<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台购物车控制器
class CartController extends Controller{
	//默认
	public function index(){
		//$_SESSION里全部商品数据
		$data = Cart::getAllData();
		if(!$data['goods']){
			View::success('购物车没有商品,快去选购吧！',U('Index/index'));
		}
		View::with('data',$data);
		//购物车页面判断用户有没有登录
		if(!isset($_SESSION['uid']) || !isset($_SESSION['username'])){
			View::error('请先登录！',U('Login/login'));
		}
		$username = $_SESSION['username'];
		View::with('username',$username);
		//统计总价
		$price =  Cart::getTotalPrice();
		View::with('price',$price);
		
		//接收去结算商品的id(确认结算)
		if(IS_POST){
			//判断有没有登录,没有登录就跳转到登录页面
			if(!isset($_SESSION['uid']) || !isset($_SESSION['username'])){
				View::error('请先登录！',U('Login/login'));
			}
			//接收post提交过来的sid(数组形式的)
			$sid = Q('post.sid');
			//1.查询对应的货品
			$data = array();
			foreach ($sid as $k => $v) {
				$data[$v] = $_SESSION['cart']['goods'][$v];
			}
			//存入$_SESSION
			$_SESSION['clearing'] = $data;
			//跳到订单页
			go(U('Indent/index',array('sid'=>$sid)));
		}
		//默认模板
	    View::make();
	}

	//删除商品
	public function del(){
		$sid = Q('get.sid');
		unset($_SESSION['cart']['goods'][$sid]);
		View::success('删除成功',U('index'));
	}
	
	//清空购物车
	public function pruge(){
	   	unset($_SESSION['cart']);
		View::success('清空成功',U('index'));
	}
	
	//异步总价
	public function total(){
	    $data = Q('post.');
		//更新购物车
		Cart::update($data); 
		$data = $_SESSION['cart']['total'];
		echo json_encode($data);exit;
	}
	
	//退出登录
	public function Logout(){
		//删除session
		session_unset();
		//销毁session
		session_destroy();
	   	go(U('Login/login'));
	}
	
}